var bg = chrome.extension.getBackgroundPage();
str = "<head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\" /> <title>popup.html</title> <style>\n<!--\n@import url(\"style.css\");\n-->  body { overflow: hidden; margin: 0px; padding: 0px; background: white; } div:first-child { margin-top: 0px; } div { cursor: pointer; text-align: center; padding: 1px 3px; font-family: sans-serif; font-size: 0.8em; width: 700px; margin-top: 1px; background: #cccccc; } div:hover { background: #aaaaaa; } #clear { border: 1px solid red; color: red; } #blue { border: 1px solid blue; color: blue; } #green { border: 1px solid green; color: green; } #yellow { border: 1px solid yellow; color: yellow; }</style> </head>";

function click(e) {
    bg.count = 0;
    window.close();
}
// send to cont_script
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
    console.log(response.farewell);	// 只有contentscript.js会打印log信息 
    if (bg) {
	document.write(str);
//    	document.write("name: " + response.cname + typeof(response.cname) + "<br>");
	for (var i = 0; i < bg.count; i++) {
		if (bg.bname[i] == response.cname)
			break;

	}
	if (i == bg.count) {
		bg.count++;
		bg.bweb[i] = response.cweb;
		bg.bname[i] = response.cname;
		bg.bsex[i] = response.csex;
		bg.bphone[i] = response.cphone;
		bg.bemail[i] = response.cemail;
	}
	document.write("<table id=\"hor-minimalist-a\" summary=\"Employee Pay Sheet\">");
	document.write("<thead>");
	document.write("<tr>");
	document.write("<th scope='col'>WEB</th>");
//	document.write("<th scope='col'>NUM</th>");
	document.write("<th scope='col'>NAME</th>");
//	document.write("<th scope='col'>SEX</th>");
	document.write("<th scope='col'>PHONE</th>");
	document.write("<th scope='col'>EMAIL</th>");
	document.write("</thead>");
	document.write("<tbody>");
	for (var j = 0; j < bg.count; j++) {
		document.write("<tr>");

//		document.write("<td>");
//		document.write(j + 1);
//		document.write("</td>");

		document.write("<td>");
		document.write(bg.bweb[j]);
		document.write("</td>");

		document.write("<td>");
		document.write(bg.bname[j]);
		document.write("</td>");

//		document.write("<td>");
//		document.write(bg.bsex[j]);
//		document.write("</td>");

		document.write("<td>");
		document.write(bg.bphone[j]);
		document.write("</td>");

		document.write("<td>");
		document.write(bg.bemail[j]);
		document.write("</td>");
		document.write("</tr>");
	}
	document.write("</tbody>");
	document.write("</table>");
    }
	document.write("<div id=\"clear\">It's all</div>");
  });
});

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    console.log(sender.tab ?
                "from a content script:" + sender.tab.url :
                "from the extension");
    if (request.greeting == "hello")
      sendResponse({farewell: "I'm popup,goodbye!"});
  });
document.addEventListener('DOMContentLoaded', function () {
		var divs = document.querySelectorAll('div');
		for (var i = 0; i < divs.length; i++) {
		divs[i].addEventListener('click', click);
		}
		});
